<?php
//single entry point

error_reporting(0);

//cookies and session init
session_set_cookie_params('3600', NULL, NULL, NULL, TRUE); //http only cookie
$lifetime = 3600;
session_start();
setcookie(session_name(),session_id(),time()+$lifetime); //1 hour after last page refresh

mb_internal_encoding('utf-8');
include '../icloud_files/system/Config.php';
include '../icloud_files/system/View.php';

if(isset($_GET['p'])) {
    $current_page = $_GET['p'];
} else {
    $current_page = 'index';
}

//echo "<pre>" . print_r($_SESSION, true) . "</pre>";

switch ($current_page) {
    case 'index':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->index();
        break;

    case 'register':
        include '../icloud_files/controllers/RegisterController.php';
        $page = new RegisterController();
        break;

    case 'login':
        include '../icloud_files/controllers/IndexController.php';
        $page = new IndexController();
        $page->login();
        break;

    case 'logout':
        include '../icloud_files/controllers/IndexController.php';
        $page = new IndexController();
        $page->logout();
        break;

    case 'dealer_search':
        include '../icloud_files/controllers/IndexController.php';
        $page = new IndexController();
        $page->dealer_search();
        break;

    case 'admin':
        include '../icloud_files/controllers/AdminController.php';
        $page = new AdminController();
        $page->index();
        break;

    case 'adv_add':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_add();
        break;

    case 'adv_view':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_view();
        break;

    case 'adv_edit':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_edit();
        break;

    case 'adv_manage':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_manage();
        break;

    case 'adv_search':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_search();
        break;

    case 'index_parts':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->index_parts();
        break;

    case 'adv_add_parts':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_add_parts();
        break;

    case 'adv_view_parts':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_view();
        break;

    case 'adv_edit_parts':
        include '../icloud_files/controllers/AddAdvController.php';
        $page = new AddAdvController();
        $page->adv_edit_parts();
        break;

    case 'about':
        include '../icloud_files/controllers/IndexController.php';
        $page = new IndexController();
        $page->about();
        break;


    //AJAX SCRIPTS
    case 'ajax_requests':
        include '../icloud_files/system/Pagination.php';
        $page = new Pagination();
        call_user_func(array($page, $_POST['method'])); //call class method dynamically
        break;

    case 'get_models':
        include '../icloud_files/models/AdvModel.php';
        $page = new AdvModel();
        $page->get_models($_GET['make'], $_GET['selected_model']);
        break;

    case 'p_adv_manage':
        include '../icloud_files/system/Pagination.php';
        $page = new Pagination();
        $page->p_adv_manage();
        break;

    case 'p_index':
        include '../icloud_files/system/Pagination.php';
        $page = new Pagination();
        $page->p_index();
        break;

    default:
        View::get_instance()->render('index', array('page_title' => 'Ahimeh Cars Home'));
}